package edu.neusoft.android.yuejian14.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;


public class RecyclerItemTouchListener implements RecyclerView.OnItemTouchListener {

    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;

    //设置Onclick事件是否拦截, 默认为拦截
    private boolean mIntercept = true;


    public interface OnItemClickListener {

        void onItemClick(View view, int position);

//        void onItemLongClick(View view, int position);
    }

    public RecyclerItemTouchListener(Context context, final RecyclerView recyclerView,
                                     OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (childView != null && mListener != null) {
                    mListener.onItemClick(childView, recyclerView.getChildLayoutPosition(childView));
                    return mIntercept;
                }
                return false;
            }

//            //长按事件
//            @Override
//            public void onLongPress(MotionEvent e) {
//                View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
//                if (childView != null && mListener != null) {
//                    mListener.onItemLongClick(childView, recyclerView.getChildLayoutPosition(childView));
//                }
//            }
        });
    }

    /**
     * 可以设置点击事件是否拦截的点击方法
     *
     * @param context      context
     * @param recyclerView RecyclerView
     * @param listener     监听接口
     * @param isIntercept  是否拦截，默认为true拦截
     */
    public RecyclerItemTouchListener(Context context, final RecyclerView recyclerView,
                                     OnItemClickListener listener, boolean isIntercept) {
        this(context, recyclerView, listener);
        this.mIntercept = isIntercept;
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        return mGestureDetector.onTouchEvent(e);
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        // do nothing
    }
}
